package com.syh.bishe.controller;


import com.syh.bishe.Util.UserHolder;
import com.syh.bishe.mapper.CarbonCreditsMapper;
import com.syh.bishe.pojo.*;
import com.syh.bishe.service.ICarbonCreditsService;
import com.syh.bishe.service.ICarbonEmissionsService;
import com.syh.bishe.service.ILogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.convert.PeriodUnit;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author syh
 * @since 2024-07-01
 */
@Tag(name = "CarbonEmissionsController", description = "碳排放模块")
@RestController
@RequestMapping("/carbon-emissions")
@RequiredArgsConstructor
public class CarbonEmissionsController {
    private final ICarbonEmissionsService carbonEmissionsService;

    @PostMapping("/add")
    @Operation(summary = "新增碳排放记录")
    public Result add(@RequestBody CarbonEmissions carbonEmissions) {
        carbonEmissions.setCompanyId(UserHolder.getUser().getCompanyId());
        return carbonEmissionsService.add(carbonEmissions);
    }

    @PutMapping("/update")
    @Operation(summary = "更新碳排放记录")
    public Result update(@RequestBody CarbonEmissions carbonEmissions) {
        return carbonEmissionsService.updateTP(carbonEmissions);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除碳排放记录")
    public Result delete(@PathVariable Integer id) {
        return carbonEmissionsService.delete(id);
    }

    @GetMapping("/get")
    @Operation(summary = "获取碳排放记录")
    public Result getData() {
        List<CarbonEmissions> data = carbonEmissionsService.getdata(UserHolder.getUser().getCompanyId());
        return Result.success(data);
    }

    @GetMapping("/gets")
    @Operation(summary = "获取所有待审核的碳排放记录")
    public Result getDatas() {
        List<CarbonEmissions> data = carbonEmissionsService.listByStatus();
        return Result.success(data);
    }

    @GetMapping("/getE1")
    @Operation(summary = "获取按月统计碳排放记录，记录可视化")
    public Result getE1() {
        List<CarbonDataWithMonth> data = carbonEmissionsService.getEcharts1(UserHolder.getUser().getCompanyId());
        return Result.success(data);
    }

    @GetMapping("/getE2")
    @Operation(summary = "获取按天统计碳排放记录，记录可视化")
    public Result getE2() {
        List<CarbonDataWithDay> data = carbonEmissionsService.getEcharts2(UserHolder.getUser().getCompanyId());
        return Result.success(data);
    }

    @GetMapping("/TPrat")
    @Operation(summary = "主页最新碳排放记录显示，记录可视化")
    public Result getTPrat() {
        DataIndex tPrat = carbonEmissionsService.getTPrat(UserHolder.getUser().getCompanyId());
        return Result.success(tPrat);
    }

    //    --------------admin审核碳排放数据------------
    @PutMapping("/update/admin")
    @Operation(summary = "审核员审核碳排放记录")
    public Result update_admin(@RequestBody CarbonEmissions carbonEmissions) {
        return carbonEmissionsService.update_admin(carbonEmissions);
    }
}
